package com.example.leaveplushjava.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.leaveplushjava.ThrowData.Result;
import com.example.leaveplushjava.domain.LeaveDomain;
import com.example.leaveplushjava.mapper.LeaveMapper;
import com.example.leaveplushjava.service.LeaveService;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 *  @Override 检查父类是否有重写的函数
 */
@Service
public class LeaveServiceImpl extends ServiceImpl<LeaveMapper, LeaveDomain> implements LeaveService {
    @Override
    public Result addLeave(LeaveDomain leaveDomain){
        Result result = new Result();
        String name = leaveDomain.getName();
        String remark = leaveDomain.getRemark();
        if(name == null || name == ""){
            return result.fail("名字不能为空");
        }
        if(remark == null || remark == ""){
            return result.fail("留言不能为空");
        }
        Long timeStamp = System.currentTimeMillis();  //获取当前时间戳
        SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss");
        String timeData = timeFormat.format(new Date(Long.parseLong(String.valueOf(timeStamp))));
        leaveDomain.setLeaveDate(timeData);

        Boolean isAdd = save(leaveDomain);
        System.out.println(isAdd);
        if(isAdd){
            return result.ok("留言成功");
        }
        return result.fail("添加失败");

    }

    @Override
    public Result listLeave(LeaveDomain leaveDomain) {
        Result result = new Result();
        String name = leaveDomain.getName();
        if(name == null || name == ""){
            return result.fail("名字不能为空");
        }
        List<LeaveDomain> leaveList = this.lambdaQuery()
                .eq(leaveDomain.getName()!=null && !leaveDomain.getName().equals(""),LeaveDomain::getName,leaveDomain.getName())
                .orderByDesc(LeaveDomain::getLeaveDate).list();
        return result.ok("查询成功").data(leaveList);
    }
}
